/*
 * SearchProvider.java
 *
 * Copyright 2009 Jack Leow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package my.edu.clhs.customsearchprovider;

import java.net.MalformedURLException;
import java.net.URL;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 * A single Search Provider.
 *
 * @author Jack Leow
 * @since December 12, 2009
 */
@ManagedBean(name="searchProvider")
@RequestScoped
public class SearchProvider {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String searchQueryUrlTemplate;

    public String getSearchQueryUrlTemplate() {
        return searchQueryUrlTemplate;
    }

    public void setSearchQueryUrlTemplate(String searchQueryUrlTemplate) {
        this.searchQueryUrlTemplate = searchQueryUrlTemplate;
    }

    private String searchSuggestionUrlTemplate;

    public String getSearchSuggestionUrlTemplate() {
        return searchSuggestionUrlTemplate;
    }

    public void setSearchSuggestionUrlTemplate(
            String searchSuggestionUrlTemplate) {
        this.searchSuggestionUrlTemplate = searchSuggestionUrlTemplate;
    }

    private String searchIconUrl;

    public String getSearchIconUrl() {
        return searchIconUrl;
    }

    public void setSearchIconUrl(String searchIconUrl) {
        this.searchIconUrl = searchIconUrl;
    }

    private boolean isValidUrl(String url) {
        try {
            new URL(url);

            return true;
        } catch (MalformedURLException e) {
            return false;
        }
    }
    
    public boolean isComplete() {
        return name != null &&

            (searchQueryUrlTemplate != null &&
            isValidUrl(searchQueryUrlTemplate)) &&

            (searchSuggestionUrlTemplate == null ||
            searchSuggestionUrlTemplate.trim().isEmpty() ||
            isValidUrl(searchSuggestionUrlTemplate)) &&

            (searchIconUrl == null || searchIconUrl.trim().isEmpty() ||
            isValidUrl(searchIconUrl));
    }
}